package com.estate.struts.actions;

import java.text.ParseException;
import java.util.List;

import com.estate.business.ContractFileService;
import com.estate.business.ContractService;
import com.estate.business.UserInfoService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.ContractVo;
import com.estate.vo.impl.UserInfoVo;

public class ContractAction extends EstateSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int id;
	private String number;
	private String name;
	private String type;
	private String clientName;
	private String clientPhone;
	private String money;
	private String signUser;
	private String signDate;
	private String signState;
	private String createUser;
	private String signPhone;
	private String signAddress;
	private String reamark;// not
	private String state;// not
	private int contractId;

	private String method;

	private String multiType;

	private ContractService contractService;
	private UserInfoService userInfoService;
	private ContractFileService contractFileService;

	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());

		String page = "";
		if ("add".equals(this.getMethod())) {
			System.out.println("add ================= " + signUser);
			ContractVo contract = this.getContract();
			if (contract != null) {
				this.getContractService().addContract(contract);
				// 到所有合同
				List<Vo> contractList = this.getContractService()
						.getAllContract();
				this.getRequest().setAttribute("contractList", contractList);

				if (this.getMultiType() != null
						&& this.getMultiType().equals("true")) {
					this.getRequest().setAttribute("multiType",
							new Boolean(true));
				} else {
					this.getRequest().setAttribute("multiType",
							new Boolean(false));
				}
				//
				page = "allContract";
			} else {
				page = "allContract";
			}
		} else if ("list".equals(this.getMethod())) {
			List<Vo> contractList = this.getContractService().getAllContract();
			this.getRequest().setAttribute("contractList", contractList);

			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}

			page = "allContract";

		} else if ("detail".equals(this.getMethod())) {
			System.out.println(this.getId());
			Vo vo = this.getContractService().getContractByPk(this.getId());
			List<Vo> voList = contractFileService.getFilesByContractId(this
					.getId());
			System.out.println(voList.size());
			if (voList.size() != 0) {
				this.getSession().setAttribute("fileList", voList);
			} else
				this.getSession().setAttribute("fileList", "还没有相关文件");
			if (vo == null) {
				page = "fail";
			} else {
				this.getRequest().setAttribute("contractVo", vo);
				page = "detail";
			}
		} else if ("modify".equals(this.getMethod())) {
			Vo vo = this.getContractService().getContractByPk(this.getId());
			if (vo == null) {
				page = "fail";
			} else {
				generateDropDownMenu();
				this.getRequest().setAttribute("contractVo", vo);
				page = "update";
			}

		} else if ("file".equals(method)) {
			ContractVo vo = (ContractVo) contractService.getContractByPk(this
					.getContractId());
			this.getSession().setAttribute("contractId", this.getContractId());
			this.getSession().setAttribute("contractName", vo.getName());
			this.getSession().setAttribute("contractNum", this.getNumber());
			page = "file";
		} else if ("update".equals(this.getMethod())) {
			ContractVo contract = this.getContract();
			contract.setId(this.getId());
			this.getContractService().updateContractVo(contract);
			page = SUCCESS;
		} else if ("delete".equals(this.getMethod())) {
			ContractVo vo = (ContractVo) this.getContractService()
					.getContractByPk(this.getId());
			System.out.println("eeee=========="
					+ this.getContractService().deleteContractVo(vo));
			page = SUCCESS;
		} else if ("addpre".equals(this.getMethod())) {
			generateDropDownMenu();
			page = "add";
		} else {
			page = "fail";
		}
		return page;
	}

	private List<Vo> generateDropDownMenu() {
		List<Vo> userList = this.getUserInfoService().getAllUserInfo();
		this.getRequest().setAttribute("userMenu", userList);
		return userList;

	}

	private ContractVo getContract() {
		ContractVo contract = new ContractVo();

		try {
			String stype = this.getType() + "";
			if (this.getNumber() == null || this.getName() == null
					|| "".equals(stype)) {
				return null;
			}
			if (this.getSignDate() != null && !"".equals(this.getSignDate())) {
				contract.setSignDate(ProjectConstants.SIMPLE_DATE.parse(this
						.getSignDate()));
			}
			contract.setNumber(number);
			contract.setName(name);
			contract.setType(type);
			contract.setClientName(clientName);
			contract.setClientPhone(clientPhone);
			contract.setMoney(money);
			// contract.setSignUser(signUser);
			System.out.println(signUser);
			UserInfoVo sUser = (UserInfoVo) this.getUserInfoService()
					.getUserInfo(Integer.parseInt(signUser));
			contract.setSignUser(sUser);
			contract.setSignState(signState);
			// contract.setCreateUser(createUser);
			UserInfoVo cUser = (UserInfoVo) this.getUserInfoService()
					.getUserInfo(Integer.parseInt(createUser));
			contract.setCreateUser(cUser);
			contract.setSignPhone(signPhone);
			contract.setSignAddress(signAddress);
			contract.setReamark(reamark);
			contract.setState(state);

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return contract;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getClientPhone() {
		return clientPhone;
	}

	public void setClientPhone(String clientPhone) {
		this.clientPhone = clientPhone;
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public String getSignUser() {
		return signUser;
	}

	public void setSignUser(String signUser) {
		this.signUser = signUser;
	}

	public String getSignDate() {
		return signDate;
	}

	public void setSignDate(String signDate) {
		this.signDate = signDate;
	}

	public String getSignState() {
		return signState;
	}

	public void setSignState(String signState) {
		this.signState = signState;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getSignPhone() {
		return signPhone;
	}

	public void setSignPhone(String signPhone) {
		this.signPhone = signPhone;
	}

	public String getSignAddress() {
		return signAddress;
	}

	public void setSignAddress(String signAddress) {
		this.signAddress = signAddress;
	}

	public String getReamark() {
		return reamark;
	}

	public void setReamark(String reamark) {
		this.reamark = reamark;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public void setContractId(int contractId) {
		this.contractId = contractId;
	}

	public int getContractId() {
		return contractId;
	}

	public void setContractFileService(ContractFileService contractFileService) {
		this.contractFileService = contractFileService;
	}

	public ContractFileService getContractFileService() {
		return contractFileService;
	}
}
